import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { ConfirmationService, SelectItem, TreeNode } from 'primeng/primeng';
import { MdlSnackbarService } from '@angular-mdl/core';
import { UnitService } from './service/unit.service';
import { switchMap } from 'rxjs/operators';

@Component({
  templateUrl: './unit-detail.component.html',
  styleUrls: ['./unit-detail.component.scss']
})
export class UnitDetailComponent implements OnInit {
  unitForm: FormGroup;
  unitInfo: any = {};
  unitTypes: SelectItem[] = new Array(
    { label: '普通部门', value: '0' },
    { label: '执法主体', value: '1' },
    { label: '实施机构', value: '2' }
  );
  unitLevels: SelectItem[] = new Array(
    { label: '省级', value: '0' },
    { label: '市级', value: '1' },
    { label: '区县级', value: '2' }
  );
  unitNatureTypes: SelectItem[] = [];//部门性质
  domains: SelectItem[] = []; //所属领域
  regions: SelectItem[] = []; //所属区划
  manubriumLevels: SelectItem[] = []; //垂管层级
  subjectTypes: SelectItem[] = [];//主体类别

  //受委托组织
  entrustTypes: SelectItem[] = [];//委托方式
  entrustNatureTypes: SelectItem[] = []; //受委托组织性质

  constructor(
    private fb: FormBuilder,
    private router: Router,
    private route: ActivatedRoute,
    private unitService: UnitService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService,
  ) { }

  ngOnInit() {
    this.buildForm();
    /** 解析URL，记录点击事件 */
    this.route.paramMap.pipe(
      switchMap((params: ParamMap) => this.unitService.getUnitInfoDetail(+params.get('id')))
    ).subscribe(unitInfo => {
      this.unitInfo = unitInfo;
      this.buildForm();
      this.mdlSnackbarService.showToast('数据加载成功！');
    });
  }

  /**点击更新或保存 */
  saveUpdateUnitInfo($event) {
    let params = this.unitForm.value;
    this.unitService.saveUpdateUnitInfo(params)
      .subscribe(data => {
        this.unitService.announceMission(data);
        this.mdlSnackbarService.showToast('数据保存成功！');
      }, error => {
        this.mdlSnackbarService.showToast('数据保存失败！');
      });
  }

  /**点击删除 */
  deleteUnitInfo($event) {
    this.confirmationService.confirm({
      header: '确认',
      message: '您确定要删除该条记录吗？',
      accept: () => {
        this.unitService.deleteUnit(this.unitInfo.unitId)
          .subscribe(data => {
            this.mdlSnackbarService.showToast('数据删除成功！');
            this.unitService.announceMission(this.unitInfo);
          }, error => {
            this.mdlSnackbarService.showToast('数据删除失败！');
          });
      }
    });
  }

  /**新增系统配置 */
  addUnitInfo($event) {
    this.unitInfo = { 'seniorId': this.unitInfo.unitId, 'seniorName': this.unitInfo.unitName };
    this.buildForm();
  }

  buildForm(): void {
    this.unitForm = this.fb.group({
      'unitId': [this.unitInfo.unitId],
      'unitCode': [this.unitInfo.unitCode],
      'unitName': [this.unitInfo.unitName],
      'shortName': [this.unitInfo.shortName],
      'officeTel': [this.unitInfo.officeTel],
      'representiveName': [this.unitInfo.representiveName],
      'socialCreditCode': [this.unitInfo.socialCreditCode],
      'unitTypeId': [this.unitInfo.unitTypeId],
      'seniorId': [this.unitInfo.seniorId],
      'seniorName': [this.unitInfo.seniorName],
      'fax': [this.unitInfo.fax],
      'postalCode': [this.unitInfo.postalCode],
      'unitAddress': [this.unitInfo.unitAddress],
      'unitLevel': [this.unitInfo.unitLevel],
      'unitNatureId': [this.unitInfo.unitNatureId]
    });
  }

}
